package org.example.Polls;

import java.io.FileOutputStream;
import java.util.Properties;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class RegisterActivity extends Activity implements OnClickListener {
	private static final String TAG = "RegisterActivity";
	
	private EditText mEmail;
	private EditText mPassword;
	private EditText mConPassword;
	private EditText mFirstName;
	private EditText mLastName;
	private EditText mAlias;
	
	private UserService userService;
	
	public RegisterActivity() {
		this.userService = DefaultServiceFactory.getInstance().createUserService();
	}
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.register);
        
    	mEmail = (EditText) findViewById(R.id.register_email_id);
    	mPassword = (EditText) findViewById(R.id.register_password_id);
    	mConPassword = (EditText) findViewById(R.id.register_confirm_passwrord);
    	mFirstName = (EditText) findViewById(R.id.register_first_name);
    	mLastName = (EditText) findViewById(R.id.register_last_name);
    	mAlias = (EditText) findViewById(R.id.register_alias_id);
        
        View main = findViewById(R.id.register_button);
        
        main.setOnClickListener(this);
        Log.d(TAG, "leaving onCreate");
    }
    
    public void onClick(View v) {
    	switch(v.getId()) {
    	case R.id.register_button:
    		
    		String password = mPassword.getText().toString();
    		String confirmPassword = mConPassword.getText().toString();
    		if (password == null || !password.equals(confirmPassword)) {
    			Log.w(TAG, "Passwords are not equal");
    		}
    		
    		User user = new User();
    		user.setEmail(mEmail.getText().toString());
    		user.setPassword(mPassword.getText().toString());
    		user.setFirstName(mFirstName.getText().toString());
    		user.setLastName(mLastName.getText().toString());
    		user.setAlias(mAlias.getText().toString());
    		
    		try {
        		//send information to system
				User newuser = userService.createUser(user);
				Log.i(TAG, "Created user with id="+newuser.getId());
				Log.i(TAG, "Created user with email="+newuser.getEmail());
				Log.i(TAG, "Created user with createDate="+newuser.getCreateDate());
				Log.i(TAG, "Created user with password="+newuser.getPassword());
				Log.i(TAG, "Created user with alias="+newuser.getAlias());
				FileOutputStream fos = openFileOutput(MainMenuActivity.CONFIG_FILE, MODE_PRIVATE);
				Properties props = new Properties();
				props.setProperty("userName", user.getEmail());
				props.setProperty("password", user.getPassword());
				props.store(fos, "uVote Configuration File");
				
				// Set the properties in the SharedPreferences as well.
				SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
				SharedPreferences.Editor editor = prefs.edit();
				editor.putString("username", user.getEmail());
				editor.putString("password", user.getPassword());
				editor.commit();
				
				Log.d(TAG, "successfully created user");
        		Log.d(TAG, "finished RegisterActivity");
        		this.finish();
			} catch (Exception e) {
				Log.d(TAG, "caught exception creating user", e);
				e.printStackTrace();
        		AlertDialog.Builder builder = new AlertDialog.Builder(this);
        		builder.setMessage(e.toString())
        		       .setCancelable(false)
        		       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        		    	   public void onClick(DialogInterface dialog, int id) {
        		    		   dialog.dismiss();
        		    	   }
        		       });
        		AlertDialog dialog = builder.create();
        		dialog.show();
			}
    		break;
    	}
    }
}